package com.example.AIstudy.controller;

import com.example.AIstudy.domain.Dialog;
import com.example.AIstudy.exception.AIstudyException;
import com.example.AIstudy.service.DialogService;
import com.example.AIstudy.util.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.ClientInfoStatus;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;

@RestController
@CrossOrigin
@RequestMapping("/dialog")
@Api(tags = "对话管理API")
public class DialogController {
    @Autowired
    DialogService dialogService;
    @ApiOperation("根据之前的对话记录和当前的问题进行回答")
    @GetMapping("")
    public String getAnswer(@ApiParam("用户id")@RequestParam(value = "userID")Integer userID,
                            @ApiParam("问题ID")@RequestParam(value = "quesID")Integer quesID,
                            @ApiParam("对话问题")@RequestParam(value = "input")String input) throws AIstudyException{

        //新建一个插入的对象
        Dialog dialog=new Dialog();
        dialog.setQuestionID(quesID);
        dialog.setAskStr(input);
        try {
                //李妃 python环境
                String anacondaUrl = "python";
                //获取python的执行文件地址
                String apiurL = new File(".").getCanonicalPath() + "/features/intelligent_qa.py";
                //传递的参数,将用户的ID传递过去，直接在python里面查询用户的信息
                String[] arg = new String[]{anacondaUrl, apiurL, input ,quesID.toString()};
                Process proc = Runtime.getRuntime().exec(arg);
                int res = proc.waitFor();
                System.out.println(res);
                if (res == 0) {
                    System.out.println("成功执行");
                    //成功执行 获取输出信息
                    String line = null;
                    BufferedReader inData = new BufferedReader(new InputStreamReader(proc.getInputStream(), "gb2312"));
                    StringBuilder builder = new StringBuilder();
                    while ((line = inData.readLine()) != null) {
                        builder.append(line);
                        builder.append("\n");
                        System.out.println(line);
                    }
                    inData.close();
                    dialog.setAnswer(builder.toString());
                    //保存这次对话记录
                    dialogService.save(dialog);
                    //返回问答的结果
                    return builder.toString();
                } else {
                    System.out.println("执行错误");
                    //未成功执行 获取错误输出信息
                    String line = null;
                    BufferedReader inError = new BufferedReader(new InputStreamReader(proc.getErrorStream(), "gb2312"));
                    while ((line = inError.readLine()) != null) {
                        System.out.println(line);
                        throw new AIstudyException(AIstudyException.ErrorType.IO_EXCEPTION, "获得智能问答结果失败");
                    }
                    inError.close();
                }
                proc.waitFor();
            } catch (IOException | InterruptedException e) {
                System.out.println(e.getMessage());
                throw new AIstudyException(AIstudyException.ErrorType.IO_EXCEPTION, "获得智能问答结果失败");
            }
            return null;
        }
}

